//
//  TIMModifyAnnounceVC.m
//  Demo_sdk
//
//  Created by Deli Xia on 2018/7/9.
//  Copyright © 2018年 Deli Xia. All rights reserved.
//

#import "TIMModifyAnnounceVC.h"
#import "TIMDemoMacro.h"

#import "TIMStorageModel.h"
#import "TIMStorageManager.h"
#import "TIMApi.h"

@interface TIMModifyAnnounceVC ()<UITextViewDelegate>
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) TIMGroupInfo *group;
@property (nonatomic, strong) NSDictionary *announceGroup;
@property (nonatomic, strong) UIButton *editBtn;
@end

@implementation TIMModifyAnnounceVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = DefaultBackGroundColor;
    self.title = @"群公告";
    [self setupNav];
    self.group = [[TIMStorageManager sharedStorageManager] getGroupInfoByGroupID:self.groupID];
    self.announceGroup = _group.grpAnnounce;
    [self.view addSubview:self.textView];
}

- (void)setupNav {
    UIButton *editBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    editBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    editBtn.selected = NO;
    editBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    
    [editBtn setTitle:@"完成" forState:UIControlStateNormal];
    
    [editBtn setTitleColor:RGBColor(190, 190, 190) forState:UIControlStateNormal];
    [editBtn setTitleColor:RGBColor(64, 145, 247) forState:UIControlStateSelected];
    self.editBtn = editBtn;
    
    [editBtn addTarget:self action:@selector(editBtnCliked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *editBtnI = [[UIBarButtonItem alloc] initWithCustomView:editBtn];
     self.navigationItem.rightBarButtonItem = editBtnI;
}

- (UITextView *)textView {
    if (!_textView) {
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 94, self.view.frame.size.width, self.view.frame.size.height - 94)];
        _textView.backgroundColor = [UIColor whiteColor];
        _textView.layer.borderWidth = 1.0;
        _textView.layer.borderColor = UIColor.blackColor.CGColor;
        _textView.text = self.group.grpAnnounceContent;
        _textView.delegate = self;
    }
    return _textView;
}

- (void)editBtnCliked {
    TIMUserInfo *user = [[TIMStorageManager sharedStorageManager] getCurrentUserInfo];
//    [TIMApi queryGroupAnnounceMentRequestWithGroupId:self.groupID grpUid:user.userID grpAnnOp:TIMAnnounceMent_Operation_GrpAnnounceMentCreate groupAnnounceContent:self.textView.text completion:^(NSError *error) {
//        if (!error) {
//            [TIMApi queryGroupAnnounceMentRequestWithGroupId:self.groupID grpUid:user.userID grpAnnOp:TIMAnnounceMent_Operation_GrpAnnounceMentGet groupAnnounceContent:nil completion:^(NSError *error) {
//                dispatch_async(dispatch_get_main_queue(), ^{
//                    [self.navigationController popViewControllerAnimated:YES];
//                });
//            }];
//        }
//    }];
    
    [TIMApi createGroupAnnnounceWithGroupId:self.groupID announceTitle:@"title" announceContent:@"text" success:^(id response) {
        NSLog(@"创建群公告成功");
        NSDictionary *dic = (NSDictionary*)response;
        [TIMApi queryGroupSingleAnnounceWithGrupId:self.groupID announceTd:dic[@"annountId"] success:^(id response) {
            NSLog(@"查询群公告成功 rsp:%@",response);
        } completion:^(NSError *error) {
            NSLog(@"查询群公告失败");
        }];
    } completion:^(NSError *error) {
         NSLog(@"创建群公告失败");
    }];
    
    self.textView.text = nil;
    [self.textView resignFirstResponder];
}

#pragma mark - TextViewDelegate

- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
    return YES;
}



@end
